package com.todo.strategy.online.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.api.client.util.Lists;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.online.infrastructure.entity.condition.StrategyInfoCondition;
import com.todo.strategy.online.infrastructure.entity.po.StrategyInfoPO;
import com.todo.strategy.online.infrastructure.mapper.StrategyInfoMapper;
import com.todo.strategy.online.infrastructure.repository.StrategyInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * (StrategyInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("strategyInfoRepository")
public class StrategyInfoRepositoryImpl implements StrategyInfoRepository {
    @Resource
    private StrategyInfoMapper strategyInfoMapper;


    @Override
    public Boolean insertStrategyInfoPO(StrategyInfoPO strategyInfoPO) {
        if (strategyInfoPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(strategyInfoPO)) {
            return false;
        }
        return strategyInfoMapper.insertStrategyInfoPO(strategyInfoPO) > 0;
    }

    @Override
    public Boolean updateStrategyInfoPOById(StrategyInfoPO strategyInfoPO) {
        if (strategyInfoPO == null || strategyInfoPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(strategyInfoPO)) {
            return false;
        }
        return strategyInfoMapper.updateStrategyInfoPOById(strategyInfoPO) > 0;
    }


    @Override
    public Boolean removeStrategyInfoPOById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return strategyInfoMapper.removeStrategyInfoPOById(id) > 0;
    }

    @Override
    public StrategyInfoPO queryStrategyInfoPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return strategyInfoMapper.queryStrategyInfoPOById(id);
    }

    @Override
    public PageResult<StrategyInfoPO> queryStrategyInfoPOByCondition(StrategyInfoCondition strategyInfoCondition) {
        PageResult<StrategyInfoPO> pageResult = new PageResult<>();
        if (strategyInfoCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(strategyInfoCondition.getPageIndex());
        pageResult.setPageSize(strategyInfoCondition.getPageSize());
        long count = queryCountByCondition(strategyInfoCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, strategyInfoCondition.getPageSize()));
        pageResult.setTotal(count);
        List<StrategyInfoPO> strategyInfoPOList = strategyInfoMapper.queryStrategyInfoByCondition(strategyInfoCondition);
        if (CollectionUtils.isEmpty(strategyInfoPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(strategyInfoPOList);
        pageResult.setCurrentIndex(strategyInfoPOList.get(0).getId());
        pageResult.setHasNext(strategyInfoPOList.size() >= strategyInfoCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(StrategyInfoCondition strategyInfoCondition) {
        if (strategyInfoCondition == null || strategyInfoCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return strategyInfoMapper.queryCountByCondition(strategyInfoCondition);
    }

    @Override
    public StrategyInfoPO queryStrategyInfoPOByPlanId(Long userId, Long planId) {
        if (userId == null || userId < 0 || planId == null || planId < 0) {
            return null;
        }
        return strategyInfoMapper.queryStrategyInfoPOByPlanId(userId, planId);
    }

    @Override
    public List<StrategyInfoPO> queryStrategyInfoByIdList(List<Long> strategyIdList) {
        if (CollectionUtils.isEmpty(strategyIdList)) {
            return Lists.newArrayList();
        }
        return strategyInfoMapper.queryStrategyInfoByIdList(strategyIdList);
    }

    @Override
    public Boolean updateStrategyStatus(Long id, StrategyStatusEnum oldStatus, StrategyStatusEnum newStatus) {
        if (id == null || oldStatus == null || newStatus == null) {
            return false;
        }
        return strategyInfoMapper.updateStrategyStatus(id, oldStatus.getType(), newStatus.getType());
    }


    private boolean baseFieldCheck(StrategyInfoPO strategyInfoPO) {
        if (strategyInfoPO == null) {
            return false;
        }
        if (strategyInfoPO.getUserId() == null || strategyInfoPO.getPlanId() == null || strategyInfoPO.getTitle() == null || strategyInfoPO.getStatus() == null) {
            log.warn("StrategyInfoRepository#baseFieldCheck 核心字段校验失败，操作失败 strategyInfoPO：【{}】", JSONObject.toJSONString(strategyInfoPO));
            return false;
        }
        if (strategyInfoPO.getSource() == null || strategyInfoPO.getMainImg() == null) {
            log.warn("StrategyInfoRepository#baseFieldCheck 基础字段校验失败，操作失败 strategyInfoPO：【{}】", JSONObject.toJSONString(strategyInfoPO));
            return false;
        }
        return true;
    }
}
